﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using System.Xml;

namespace Guru.Web.Controls
{
    internal static class HTMLTemplate
    {
        private static string _HTMLTemplaces;
        private static XmlDocument _XmlDocument;

        public static string HTMLTemplaces
        {
            get 
            {
                if (_HTMLTemplaces == null)
                {
                    using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Guru.Web.Controls.EmbeddedResource.HTMLTemplates.xml"))
                    {
                        using (StreamReader reader = new StreamReader(stream))
                        {
                            _HTMLTemplaces = reader.ReadToEnd();
                        }
                    }

                }
                return _HTMLTemplaces;
            }
            set 
            {
                _HTMLTemplaces = value;
            }
        }

        public static string GetByKey(string key)
        {
            if (_XmlDocument == null)
            {
                _XmlDocument = new XmlDocument();
                _XmlDocument.LoadXml(HTMLTemplaces);
            }
            XmlNode node = _XmlDocument.SelectSingleNode(string.Format("//Templace[@Key=\"{0}\"]", key));
            if (node == null) return string.Empty;
            else return node.InnerXml.Replace("&amp;", "&");           
        }
        public static string GetByType(Type type) 
        {
            return GetByKey(type.ToString());
        }

    }
}
